def qsort(nums,i,j):
    if i>j:
        return

    l = i
    r = j
    baseIdx = l
    baseNum = nums[l]
    
    while l<r:
        while nums[r]>=baseNum and l<r:
            r = r - 1

        while nums[l]<=baseNum and l<r:
            l = l + 1


        if l<r:
            t = nums[l]
            nums[l]=nums[r]
            nums[r]=t

    t = nums[l]
    nums[l]=baseNum
    nums[baseIdx]=t

    qsort(nums,i,l-1)
    qsort(nums,l+1,j)
    
nums=[6,4,1,3,45,7,9,22,12,8,786,123]
qsort(nums,0,len(nums)-1)
print(nums)